<html>
<head><meta charset="utf-8"><title>ptr to array using array indexing · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html">ptr to array using array indexing</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220928518"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220928518" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220928518">(Dec 25 2020 at 17:02)</a>:</h4>
<p>As far as I know the following snippet is unsound because <code>y</code> must only be used to access <code>x[0]</code>, not the whole array.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">test</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kt">i32</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Is my knowledge correct here and where can I read more about this?</p>



<a name="220928725"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220928725" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220928725">(Dec 25 2020 at 17:09)</a>:</h4>
<p>There was a thread about this but I can't find it.</p>



<a name="220928778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220928778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220928778">(Dec 25 2020 at 17:10)</a>:</h4>
<p>Under the stacked borrows memory model this is indeed UB. (not unsound. that is when you can misuse an api to cause UB without using unsafe code for the api calls)</p>



<a name="220928839"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220928839" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220928839">(Dec 25 2020 at 17:12)</a>:</h4>
<blockquote>
<p>not unsound. that is when you can misuse an api to cause UB without using unsafe code for the api calls</p>
</blockquote>
<p>the existence of that method is unsound, isn't it? It's only when calling it when it ends up being ub afaik</p>



<a name="220928841"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220928841" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> comex <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220928841">(Dec 25 2020 at 17:12)</a>:</h4>
<p><a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/134">https://github.com/rust-lang/unsafe-code-guidelines/issues/134</a> &lt;- here it is</p>



<a name="220958774"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220958774" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220958774">(Dec 26 2020 at 10:27)</a>:</h4>
<p>soundness is a property of a safe API surface, so probably not the right question here</p>



<a name="220958815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220958815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220958815">(Dec 26 2020 at 10:28)</a>:</h4>
<p>but if you consider this a weird module that just exposes <code>pub fn test()</code>, it is an unsound module</p>



<a name="220958821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/220958821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#220958821">(Dec 26 2020 at 10:28)</a>:</h4>
<p>but given that this is an argumentless function, it's IMO easier to just consider it a program and ask if that program has UB</p>



<a name="221158695"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221158695" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221158695">(Dec 29 2020 at 19:11)</a>:</h4>
<p>This seems like a syntax ambiguity of the type that raw references might help to address. In the absence of raw references, do we have any syntax to create a pointer to a specific element of a slice without restricting the pointer to operate within just that element?</p>



<a name="221158838"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221158838" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221158838">(Dec 29 2020 at 19:12)</a>:</h4>
<p>I suppose you could create a pointer to the slice, and then offset with manual indexing.</p>



<a name="221158885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221158885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221158885">(Dec 29 2020 at 19:13)</a>:</h4>
<p>But at least in C, it's conventional to write <code>&amp;arr[i]</code> and let the compiler do the indexing for you.</p>



<a name="221158974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221158974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221158974">(Dec 29 2020 at 19:14)</a>:</h4>
<p>It seems unfortunate if following the same pattern would produce UB.</p>



<a name="221160736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221160736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221160736">(Dec 29 2020 at 19:39)</a>:</h4>
<p>yeah, i've found that unfortunate too. some of the discussions towards the end of <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/256">https://github.com/rust-lang/unsafe-code-guidelines/issues/256</a> <em>might</em> indicate a path forward there, but it's not 100% clear if that's actually true or just wishful thinking.</p>
<p>that said, thankfully this isn't that hard of a rule to work around in practice.</p>



<a name="221167318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221167318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221167318">(Dec 29 2020 at 21:10)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing/near/221158838">said</a>:</p>
<blockquote>
<p>I suppose you could create a pointer to the slice, and then offset with manual indexing.</p>
</blockquote>
<p>yeah, <code>slice.as_ptr().add(idx)</code> is what I currently recommend</p>



<a name="221167333"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221167333" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221167333">(Dec 29 2020 at 21:10)</a>:</h4>
<p>but of course nobody does that since it's way longer</p>



<a name="221167690"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221167690" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221167690">(Dec 29 2020 at 21:15)</a>:</h4>
<p>How many optimizations would we lose making the raw indexing be sugar for that? Or is it a consistency thing with <code>Index(Mut)</code>?</p>



<a name="221167824"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221167824" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221167824">(Dec 29 2020 at 21:17)</a>:</h4>
<p>there's no such thing as "raw indexing"?</p>



<a name="221169005"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221169005" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221169005">(Dec 29 2020 at 21:30)</a>:</h4>
<p>You could also do <code>slice[idx..].as_ptr()</code> if you just want access to the latter half of the list</p>



<a name="221176143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221176143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Quy Nguyen <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221176143">(Dec 29 2020 at 23:21)</a>:</h4>
<p>Is <code>&amp;a[b]</code> on slices not a primitive operation?</p>



<a name="221190772"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221190772" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mario Carneiro <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221190772">(Dec 30 2020 at 05:11)</a>:</h4>
<p><code>slice[idx..].as_ptr()</code> desugars to <code>slice.index(idx..).as_ptr()</code>, where the <code>index</code> call is not special but produces a subslice, and <code>as_ptr()</code> produces a pointer which is permitted to access everything in that subslice</p>



<a name="221194621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221194621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221194621">(Dec 30 2020 at 07:04)</a>:</h4>
<p>Sounds like we might need some special syntax for &amp;raw arr[i] when we go to add syntax for &amp;raw.</p>



<a name="221194635"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221194635" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Josh Triplett <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221194635">(Dec 30 2020 at 07:04)</a>:</h4>
<p>It's reasonable to tell people they have to use &amp;raw instead of &amp; for something like this.</p>



<a name="221195143"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221195143" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221195143">(Dec 30 2020 at 07:18)</a>:</h4>
<p>Here's a starter macro to let you test if you super care about being able to make pointer indexing look like normal indexing.</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="sd">/// Performs "pointer indexing" via the `add` method</span>
<span class="fm">macro_rules!</span><span class="w"> </span><span class="n">pi</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="p">(</span><span class="cp">$array</span>:<span class="nc">ident</span><span class="w"> </span><span class="p">[</span><span class="w"> </span><span class="cp">$index</span>:<span class="nc">expr</span><span class="w"> </span><span class="p">])</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="cp">$array</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="cp">$index</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="k">mut</span><span class="w"> </span><span class="n">data</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">7</span><span class="p">,</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"> </span><span class="mi">9</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">data_ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">data</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">pi</span><span class="o">!</span><span class="p">(</span><span class="n">data_ptr</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">20</span><span class="p">;</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"{}"</span><span class="p">,</span><span class="w"> </span><span class="n">pi</span><span class="o">!</span><span class="p">(</span><span class="n">data_ptr</span><span class="p">[</span><span class="mi">2</span><span class="p">]));</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221209543"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221209543" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221209543">(Dec 30 2020 at 12:37)</a>:</h4>
<p><span class="user-mention silent" data-user-id="239881">Josh Triplett</span> <a href="#narrow/stream/136281-t-lang.2Fwg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing/near/221194621">said</a>:</p>
<blockquote>
<p>Sounds like we might need some special syntax for &amp;raw arr[i] when we go to add syntax for &amp;raw.</p>
</blockquote>
<p>ah that's what you mean... well I think that should actually already work? Did someone test using <code>ptr::raw_const!(arr[i])</code> in Miri?</p>



<a name="221209588"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221209588" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221209588">(Dec 30 2020 at 12:38)</a>:</h4>
<p>(btw, any suggestions for better names for <code>ptr::raw_const!</code>? See <a href="https://github.com/rust-lang/rust/issues/73394#issuecomment-751342185">https://github.com/rust-lang/rust/issues/73394#issuecomment-751342185</a> )</p>



<a name="221209678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221209678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221209678">(Dec 30 2020 at 12:40)</a>:</h4>
<p>this also fails in Miri:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(raw_ref_macros)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">ptr</span>::<span class="n">raw_const</span><span class="o">!</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>I think that's because the ref-to-raw conversion happens after the indexing.</p>



<a name="221211784"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221211784" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221211784">(Dec 30 2020 at 13:20)</a>:</h4>
<p>this works:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#![feature(raw_ref_macros)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">[</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="mi">42</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="n">x</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="p">[</span><span class="kt">i32</span><span class="p">;</span><span class="w"> </span><span class="mi">2</span><span class="p">];</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span>: <span class="o">*</span><span class="k">const</span><span class="w"> </span><span class="kt">i32</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">std</span>::<span class="n">ptr</span>::<span class="n">raw_const</span><span class="o">!</span><span class="p">((</span><span class="o">*</span><span class="n">x</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">y</span><span class="p">.</span><span class="n">add</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>



<a name="221212153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221212153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221212153">(Dec 30 2020 at 13:26)</a>:</h4>
<p>bummer, it certainly would be nice if the first of those worked.</p>



<a name="221212273"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221212273" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221212273">(Dec 30 2020 at 13:29)</a>:</h4>
<p>yeah, but that'll require the things discussed in <a href="https://github.com/rust-lang/unsafe-code-guidelines/issues/256">https://github.com/rust-lang/unsafe-code-guidelines/issues/256</a></p>



<a name="221213119"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221213119" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Thom Chiovoloni <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221213119">(Dec 30 2020 at 13:42)</a>:</h4>
<p>ah, yeah, fair enough</p>



<a name="221252425"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr%20to%20array%20using%20array%20indexing/near/221252425" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/ptr.20to.20array.20using.20array.20indexing.html#221252425">(Dec 30 2020 at 22:35)</a>:</h4>
<p>I think that indexing is one of those things, like addition, that only sometimes goes through the trait.  Which makes this potentially a bit more awkward, as things that work on slices might not work on vectors.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>